//
//  News.swift
//  lookLikeIweekly
//
//  Created by Mainzxq on 16/6/3.
//  Copyright © 2016年 Mainzxq. All rights reserved.
//

import Foundation
import UIKit

protocol PictureNewsProtocol {
    func getImage() -> UIImage
}

// 新闻基类
class News:NSObject, NSCoding {
    var appName: String
    var comments: String
    
    override init() {
        self.appName = "仿iWeekly新闻应用"
        self.comments = "根据课程要求，分别需要制作图片轮播控制器、仿制新闻页面、仿制聊天页面、仿制微博页面"
    }
    
    
    required init(coder aDecoder: NSCoder) {
        self.appName = aDecoder.decodeObjectForKey("appName") as! String
        self.comments = aDecoder.decodeObjectForKey("comments") as! String
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(self.appName, forKey: "appName")
        aCoder.encodeObject(self.comments, forKey: "comments")
    }
    
}

// 主文章类，主心骨类
class ArticleSection: News {
    var title: String = ""
    var picName: String = ""
    var picAddress: String = ""
    var newsTime: NSDate = NSDate()
    var article: String = ""
    var writer: String = ""
    var photographer: String = ""
    
    
    init(artiDict: NSDictionary) {
        self.title = artiDict["title"] as! String
        self.picName = artiDict["picName"]  as! String
        self.picAddress = artiDict["picAddress"] as! String
        self.newsTime = stringToDate(artiDict["newsTime"] as! String)
        self.article = artiDict["article"] as! String
        self.writer = artiDict["writer"] as! String
        self.photographer = artiDict["photographer"] as! String
        super.init()
    }
    
    required init(coder aDecoder: NSCoder) {
        self.title = aDecoder.decodeObjectForKey("title") as! String
        self.picName = aDecoder.decodeObjectForKey("picName") as! String
        self.picAddress = aDecoder.decodeObjectForKey("picAddress") as! String
        self.newsTime = aDecoder.decodeObjectForKey("newsTime") as! NSDate
        self.article = aDecoder.decodeObjectForKey("article") as! String
        self.writer = aDecoder.decodeObjectForKey("writer") as! String
        self.photographer = aDecoder.decodeObjectForKey("photographer") as! String
        super.init(coder: aDecoder)
    }
    
    override func encodeWithCoder(aCoder: NSCoder) {
        super.encodeWithCoder(aCoder)
        aCoder.encodeObject(self.title, forKey: "title")
        aCoder.encodeObject(self.picName, forKey: "picName")
        aCoder.encodeObject(self.picAddress, forKey: "picAddress")
        aCoder.encodeObject(self.newsTime, forKey: "newsTime")
        aCoder.encodeObject(self.article, forKey: "article")
        aCoder.encodeObject(self.writer, forKey: "writer")
        aCoder.encodeObject(self.photographer, forKey: "photographer")
        
    }
}


// 图片类增加了图片下载的方法
class PictureArticleSection: ArticleSection, PictureNewsProtocol {
    var albumImage: UIImage?
    init(picDict: NSDictionary) {
        self.albumImage = nil
        super.init(artiDict: picDict)
        
    }
    
    required init(coder aDecoder: NSCoder){
        self.albumImage = aDecoder.decodeObjectForKey("albumImage") as? UIImage
        super.init(coder: aDecoder)
        
    }
    
    override func encodeWithCoder(aCoder: NSCoder) {
        super.encodeWithCoder(aCoder)
        aCoder.encodeObject(self.albumImage, forKey: "albumImage")
    }
    
    // 从网络下载图片
    func getImage() -> UIImage {
        let url = NSURL(string: self.picAddress)
        let imgData = UIImage(data:NSData(contentsOfURL: url!)!)
        self.albumImage = imgData!
        return imgData!
    }
}


class People {
    var name: String
    var age: Int
    var sex: String
    var phone: String
    
    init(name: String, age: Int, sex: String, phone: String){
        self.name = name
        self.age = age
        self.sex = sex
        self.phone = phone
    }
}

class Member: People {
    var password: String
    var authLevel: Int

    init(name: String, age: Int, sex: String, phone: String, password: String, authlevel: Int) {
        self.password = password
        self.authLevel = authlevel
        super.init(name: name, age: age, sex: sex, phone: phone)
    }
}


func dateToString(date: NSDate) -> String {
    let dateFormater = NSDateFormatter()
    dateFormater.dateFormat = "yyyy年 MM月 dd日"
    let stringDate = dateFormater.stringFromDate(date)
    return stringDate
}


func stringToDate(date: String) -> NSDate {
    let dateFormater = NSDateFormatter()
    dateFormater.dateFormat = "yyyy年 MM月 dd日"
    let nsDate = dateFormater.dateFromString(date)
    return nsDate!
}







